﻿using OneSpider.Models.ResultModel;
using OneSpider.Helper;  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OneSpider.API
{
    public class ApiService
    {
        /// <summary>
        /// 获取特定日期的首页
        /// </summary>
        /// <param name="date">2016-08-01</param>
        public static async Task<List<ContentResult>> GetMainPageAsync(string date)
        {
            string json = await HttpBaseService.GetJsonAsync(string.Format(ServiceUri.Content, date));
            if (!string.IsNullOrEmpty(json))
            {
                var result = JsonHelper.Deserlialize<ResultTemplate<List<ContentResult>>>(json);
                return result.Data;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取特定日期的连载
        /// </summary>
        /// <param name="date">2016-08-01</param>
        public static async Task<List<SerialResult>> GetSerialAsync(string date)
        {
            string json = await HttpBaseService.GetJsonAsync(string.Format(ServiceUri.Serial, date));
            if (!string.IsNullOrEmpty(json))
            {
                var result = JsonHelper.Deserlialize<ResultTemplate<List<SerialResult>>>(json);
                return result.Data;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取特定日期的文章
        /// </summary>
        /// <param name="date">2016-08-01</param>
        public static async Task<List<ArticleResult>> GetEssayAsync(string date)
        {
            string json = await HttpBaseService.GetJsonAsync(string.Format(ServiceUri.Essay, date));
            if (!string.IsNullOrEmpty(json))
            {
                var result = JsonHelper.Deserlialize<ResultTemplate<List<ArticleResult>>>(json);
                return result?.Data;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取指定Id的文章内容
        /// </summary>
        /// <param name="date">2016-08-01</param>
        public static async Task<ArticleDetailResult> GetEssayDetailAsync(int contentId)
        {
            string json = await HttpBaseService.GetJsonAsync(string.Format(ServiceUri.EssayDetail, contentId));
            if (!string.IsNullOrEmpty(json))
            {
                var result = JsonHelper.Deserlialize<ResultTemplate<ArticleDetailResult>>(json);
                return result.Data;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取特定日期的音乐
        /// </summary>
        /// <param name = "date" > 2016 - 08 - 01 </ param >
        //public static async Task<List<Music>> GetMusicAsync(string date)
        //{
        //    string json = await HttpBaseService.GetJsonAsync(string.Format(ServiceUri.Music, date));
        //    if (!string.IsNullOrEmpty(json))
        //    {
        //        var result = JsonHelper.Deserlialize<ResultTemplate<List<Music>>>(json);
        //        return result.Data;
        //    }
        //    else
        //    {
        //        return null;
        //    }
        //}

        /// <summary>
        /// 获取特定日期的问答
        /// </summary>
        /// <param name="date">2012-10-01</param>
        public static async Task<List<AnswerResult>> GetAnswerAsync(string date)
        {
            string json = await HttpBaseService.GetJsonAsync(string.Format(ServiceUri.QA, date));
            if (!string.IsNullOrEmpty(json))
            {
                var result = JsonHelper.Deserlialize<ResultTemplate<List<AnswerResult>>>(json);
                return result.Data;
            }
            else
            {
                return null;
            }
        }

        public static async Task<AnswerDetailResult> GetAnswerDetailAsync(int questionId)
        {
            string json = await HttpBaseService.GetJsonAsync(string.Format(ServiceUri.QADetail, questionId));
            if (!string.IsNullOrEmpty(json))
            {
                var result = JsonHelper.Deserlialize<ResultTemplate<AnswerDetailResult>>(json);
                return result.Data;
            }
            else
            {
                return null;
            }
        }
    }
}
